package net.dgg.rz.api.production.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.rz.api.production.service.WorkBentchService;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.excption.CommonExcption;
import net.dgg.rz.common.service.CommonMangerExtService;
import net.dgg.rz.common.utils.DateUtils;
import net.dgg.rz.common.utils.StringUtils;
import net.dgg.tmd.foundation.platform.datapermission.entity.DataAuthDTO;
import net.dgg.tmd.foundation.platform.datapermission.service.DataPermissionAssocService;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/rzsc/workbench")
public class WorkBentchController extends DggBaseController {
    @Autowired
    private WorkBentchService workBentchService;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private DataPermissionAssocService dataPermissionAssocService;
    @Autowired
    private CommonMangerExtService commonMangerExtService;

    @RequestMapping("/get_workbench_num")
    @ResponseBody
    public DggRestResponse getWorkBentchNum(@RequestParam Map params) {
        UserEntity user = sessionManager.getCurrentUser();
        Map map = new HashMap();
        Integer resultNum = null;
        String code =params.get("code").toString();
        DataAuthDTO dataAuthDTO = dataPermissionAssocService.selectAuthToConfigureRturnOrgIds("RZSC_ORDER_DEPT_DATA");
            if (dataAuthDTO != null && dataAuthDTO.getOrgIds() != null && dataAuthDTO.getOrgIds().size()>0 && dataAuthDTO.getIsAll() != null && dataAuthDTO.getIsAll().longValue() != 1) {
                map.put("pageOrgId", dataAuthDTO.getOrgIds());
            }
            if (null != user && null != user.getId()&&dataAuthDTO != null && dataAuthDTO.getIsAll() != null && dataAuthDTO.getIsAll().longValue() != 1) {
                map.put("pageUserId", user.getId());
                map.put("code", code);
                try {
                    resultNum = workBentchService.getWorkBentchNum(map);
                } catch (Exception e) {
                    e.printStackTrace();
                    return getFailResponse("操作失败!");
                }
            } else {
                return getFailResponse("操作失败!");
            }
            return getSuccessResponse(resultNum);
    }

    /**
     *  统计接单、废单、退单、放款、放款金额、已完结（今日、当月、上月）
     *  @title countRzData
     *  @param params (code 状态区分接单，废单等，countTimeType 0 今天 1 本月 2 上个月)
     */
    @RequestMapping("/countRzDate")
    @ResponseBody
    @SuppressWarnings("all")
    public DggRestResponse countRzData(@RequestParam("code") String code,@RequestParam(value = "countTimeType", required = false) String countTimeType){
        String resultNum = "0";//里面有统计总条数的有统计总金额的 所以用了String
        Map<String, String> params = new HashMap<>();
        params.put("code", code);
        params.put("countTimeType", countTimeType);
        try {
            resultNum = workBentchService.countRzData(params);
        }catch (CommonExcption e){
            e.printStackTrace();
            return getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return getFailResponse("查询失败,稍后重试!");
        }
        return getSuccessResponse(resultNum);
    }

    /**
     * 工作台跳转待接收页面（转单待接收，下单待接收，退回待接收）
     *
     */
    @RequestMapping("/waitReceive")
    public String waitReceive(@RequestParam(required = true) String code,Model model){
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        model.addAttribute("code",code);
        return "rzsc/processingorder/processing_order";
    }

    /**
     * 已分配
     * @param code 信用贷款  抵押贷款 资金业务  其他  全部
     * @param timeType 0 今日 1本月
     */
    @RequestMapping("/distribution")
    public String distribution(@RequestParam(required = false) String code,@RequestParam(value="countTimeType" ,required = true) String timeType,Model model){
        Map map = new HashMap();
        if(StringUtils.isNotEmpty(code)){
            map = commonMangerExtService.getTreeBookObj(code);
        }
        StringBuffer sb = new StringBuffer();
        if("0".equals(timeType)){
           String timeStr = DateUtils.getDayOfThisDay(new Date());
            sb.append(timeStr).append(" ~ ").append(timeStr);
        }else if("1".equals(timeType)){
            String[] timeStr = DateUtils.thisMonthFirstAndLast();
            sb.append(timeStr[0]).append(" ~ ").append(timeStr[1]);
        }
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        model.addAttribute("code",code);
        model.addAttribute("codeName", StringUtils.isEmpty(map) ? "" : map.get("name"));
        model.addAttribute("timeType",sb);
        return "rzsc/allocation/allocate_record";
    }

    /**
     * 待分配
     */
    @RequestMapping("/waitDistribution")
    public String waitDistribution(Model model){
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "rzsc/allocation/allocate";
    }

    /**
     * 已完结(已退订单，已废单，已完结)
     * @param code
     * @param timeType 0今天 1本月 2上个月
     */
    @RequestMapping("/completed")
    @SuppressWarnings("all")
    public String completed(Model model,@RequestParam(required = true) String code,@RequestParam(value="countTimeType" ,required = true) String timeType){
        StringBuffer sb = new StringBuffer();
        if("0".equals(timeType)){
            String timeStr = DateUtils.getDayOfThisDay(new Date());
            sb.append(timeStr).append(" ~ ").append(timeStr);
        }else if("1".equals(timeType)){
            String[] timeStr = DateUtils.thisMonthFirstAndLast();
            sb.append(timeStr[0]).append(" ~ ").append(timeStr[1]);
        }else if("2".equals(timeType)){
           String[] timeStr =  DateUtils.lastMonthFirstAndLast();
            sb.append(timeStr[0]).append(" ~ ").append(timeStr[1]);
        }
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        model.addAttribute("code",code);
        model.addAttribute("timeType",sb);
        return "rzsc/order/completed_order";
    }

    /**
     * 已放款
     */
    @RequestMapping("/loaned")
    @SuppressWarnings("all")
    public String loadned(Model model,@RequestParam(value="countTimeType" ,required = true) String timeType){
        StringBuffer sb = new StringBuffer();
        if("0".equals(timeType)){
            String timeStr = DateUtils.getDayOfThisDay(new Date());
            sb.append(timeStr).append(" ~ ").append(timeStr);
        }else if("1".equals(timeType)){
            String[] timeStr = DateUtils.thisMonthFirstAndLast();
            sb.append(timeStr[0]).append(" ~ ").append(timeStr[1]);
        }else if("2".equals(timeType)){
            String[] timeStr =  DateUtils.lastMonthFirstAndLast();
            sb.append(timeStr[0]).append(" ~ ").append(timeStr[1]);
        }
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        model.addAttribute("timeType",sb);
        return "rzsc/processingorder/processing_order";
    }

//    工作台页面数据测试使用
    @RequestMapping("/work_bentch_test.html")
    public String workBentchTest(){
        return  "rzsc/demo/work_bentch_page";
    }

    /**
     * 超期统计(分配，订单，接受)
     */
    @RequestMapping("/beyondCount")
    @ResponseBody
    @SuppressWarnings("all")
    public DggRestResponse beyondCount(@RequestParam("code") String code,@RequestParam(value = "countTimeType", required = false) String countTimeType){
        int resultNum = 0;
        Map<String, String> params = new HashMap<>();
        params.put("code", code);
        try {
            resultNum = workBentchService.beyondCount(params);
        }catch (CommonExcption e){
            e.printStackTrace();
            return getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return getFailResponse("查询失败,稍后重试!");
        }
        return getSuccessResponse(resultNum);
    }

    /**
     * 暂缓
     */
    @RequestMapping("/suspended")
    public String suspended(Model model){
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        model.addAttribute("codes",ScProductConstant.RZSCORDER_STATUS03);
        return "rzsc/processingorder/processing_order";
    }

    /**
     * 待审核（废单,退单）
     */
    @RequestMapping("/waitAudit")
    public String waitAudit(Model model,@RequestParam(required = true) String code){
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        model.addAttribute("code",code);
        return "rzsc/auditorder/audit_order";
    }

}
